<?php
require_once('config.scenic.php');
/** 
 * 有关景区的参数设置
 */
//require_once('config.mgafinstall.php');

class Scenic_scenicController extends Gaf_Controller_Action {
	private $scenic;
	public function init(){
		parent::init();
		$this->scenic=new scenic();
	}
	/**
	 * @name 用户审核
	 * @menu 景区管理/景区列表
	 * @comment 景区管理，设置景区参数。
	 * @type WEB
	 */
	public function indexAction(){
		$p=$this->_getParam("p","0");
		$scenicList=$this->scenic->getScenicList();
		$this->view->MsgSection=$this->_getParam('errmsg');
		$this->view->sceniclist=$scenicList;
		$this->view->page=$p;
	}
	/**
	 * @name 增加景区
	 * @comment 增加景区
	*/
	public function addscenicAction(){
		$rlist=$this->scenic->getRegionByLevel();
		$this->view->rlist=$rlist;
	}
	public function doaddscenicAction(){
		$this->_helper->viewRenderer->setNoRender();
		$paramArr=array();
		$paramArr['name']=$name=$this->_getParam('name');
		$paramArr['level']=$level=$this->_getParam('level');
		$paramArr['recommend']=$recommend=$this->_getParam('recommend');
		$paramArr['addr']=$addr=$this->_getParam('addr');
		$paramArr['regiondesc']=$regiondesc=$this->_getParam('regiondesc');
		$paramArr['weather']=$weather=$this->_getParam('weather');
		$paramArr['environment']=$environment=$this->_getParam('environment');
		$paramArr['travelguide']=$travelguide=$this->_getParam('travelguide');
		$paramArr['review']=$review=$this->_getParam('review');
		$paramArr['district']=$district=$this->_getParam('district');
		$paramArr['city']=$city=$this->_getParam('city');
		$paramArr['province']=$province=$this->_getParam('province');
		$paramArr['area']=$area=$this->_getParam('area');
		$paramArr['foreign']=$foreign=$this->_getParam('foreign');
		$ret=$this->scenic->addScenic($paramArr);
		if($ret)	$this->_redirect("/scenic/scenic/index?errmsg=添加成功");
		else $this->_redirect("/scenic/scenic/index?errmsg=添加失败");
	}
	/*取景区需要修改的信息*/
	public function getscenicAction(){
		$sid=$this->_getParam('sid');
		$scenic=$this->scenic->getScenicById($sid);
		$reg2scenic=$this->scenic->getRegion2Scenic($sid);
		$rslist=$this->scenic->getRegionByLevel();
		foreach ($rslist as &$r){
			$r['sel']='';
			foreach ($reg2scenic as $rg){
				if($r['rid']==$rg['rid'])	$r['sel']="selected";
				else if($r['sel']!='selected') $r['sel']='';
			}
		}
		$this->view->scenic=$scenic;
		$this->view->rslist=$rslist;
	}
	/**
	 * @name 修改景区
	 * @comment 修改景区
	*/
	public function editscenicAction(){
		$this->_helper->viewRenderer->setNoRender();
		$paramArr=array();
		$sid=$this->_getParam('sid');
		$paramArr['name']=$name=$this->_getParam('name');
		$paramArr['level']=$level=$this->_getParam('level');
		$paramArr['recommend']=$recommend=$this->_getParam('recommend');
		$paramArr['addr']=$addr=$this->_getParam('addr');
		$paramArr['regiondesc']=$regiondesc=$this->_getParam('regiondesc');
		$paramArr['weather']=$weather=$this->_getParam('weather');
		$paramArr['environment']=$environment=$this->_getParam('environment');
		$paramArr['travelguide']=$travelguide=$this->_getParam('travelguide');
		$paramArr['review']=$review=$this->_getParam('review');
		$paramArr['district']=$district=$this->_getParam('district');
		$paramArr['city']=$city=$this->_getParam('city');
		$paramArr['province']=$province=$this->_getParam('province');
		$paramArr['area']=$area=$this->_getParam('area');
		$paramArr['foreign']=$foreign=$this->_getParam('foreign');
		
		$ret=$this->scenic->editScenicById($sid,$paramArr);
		if($ret)	$this->_redirect("/scenic/scenic/index?errmsg=修改成功");
		else $this->_redirect("/scenic/scenic/index?errmsg=修改失败");
	}
	/*增加景区相册*/
	public function addgalleryAction(){
		$sid=$this->_getParam('sid');
		$gallery=$this->scenic->getGalleryFromScenic($sid);
		$this->view->sid=$sid;
		$this->view->gallery=$gallery;
	}
	/**
	 * @name  查看门票列表
	 * @comment 查看门票列表
	*/
	public function addticketAction(){
		$p=$this->_getParam("p","0");
		$sid=$this->_getParam('sid');
		$this->view->MsgSection=$this->_getParam('errmsg');
		$this->view->sid=$sid;
		$ticketList=$this->scenic->getTicketListFromScenic($sid);
		$this->view->ticketlist=$ticketList;
		$this->view->page=$p;
	}
	/**
	 * @name 添加门票
	 * @comment 添加门票
	*/
	public function doaddticketAction(){
		$this->_helper->viewRenderer->setNoRender();
		
		$paramArr['sid']=$sid=$this->_getParam('sid');
		$paramArr['name']=$name=$this->_getParam('name');
		$paramArr['type']=$type=$this->_getParam('type');
		$paramArr['ptype']=$ptype=$this->_getParam('ptype');
		$paramArr['season']=$season=$this->_getParam('season');
		$paramArr['pricetype']=$pricetype=$this->_getParam('pricetype');
		$paramArr['recommend']=$recommend=$this->_getParam('recommend');
		$paramArr['price']=$price=$this->_getParam('price');
		$paramArr['realprice']=$realprice=$this->_getParam('realprice');
		$paramArr['memo']=$memo=$this->_getParam('memo');
		$paramArr['memodetail']=$memodetail=$this->_getParam('memodetail');
	
		//debug::output(1,$paramArr);
		$ret=$this->scenic->addTicket($paramArr);
		if($ret)	$this->_redirect("/scenic/scenic/addticket?sid=$sid&errmsg=添加成功");
		else $this->_redirect("/scenic/scenic/addticket?sid=$sid&errmsg=添加失败");
	}
	public function delticketAction(){
		$this->_helper->viewRenderer->setNoRender();
		
		$sid=$this->_getParam('sid');
		$tid=$this->_getParam('tid');
		$ret=$this->scenic->delTicketById($tid);
		if($ret)	$this->_redirect("/scenic/scenic/addticket?sid=$sid&errmsg=删除成功");
		else $this->_redirect("/scenic/scenic/addticket?sid=$sid&errmsg=删除失败");
	}
	/**
	 *@name 查看景区门票信息
	 * @comment 查看景区门票信息 
	*/
	public function getticketAction(){
		$tid=$this->_getParam('tid');
		$ticket=$this->scenic->getTicketById($tid);
		$this->view->ticket=$ticket;
	}
	/**
	 * @name  修改门票
	 * @comment  修改门票
	*/
	public function editticketAction(){
		$tid=$this->_getParam('tid');
		$sid=$this->_getParam('sid');
		$paramArr['name']=$name=$this->_getParam('name');
		$paramArr['type']=$type=$this->_getParam('type');
		$paramArr['ptype']=$ptype=$this->_getParam('ptype');
		$paramArr['season']=$season=$this->_getParam('season');
		$paramArr['pricetype']=$pricetype=$this->_getParam('pricetype');
		$paramArr['recommend']=$recommend=$this->_getParam('recommend');
		$paramArr['price']=$price=$this->_getParam('price');
		$paramArr['realprice']=$realprice=$this->_getParam('realprice');
		$paramArr['memo']=$memo=$this->_getParam('memo');
		$paramArr['memodetail']=$memodetail=$this->_getParam('memodetail');
		
		$ret=$this->scenic->editTicketById($tid,$paramArr);
		if($ret)	$this->_redirect("/scenic/scenic/addticket?sid=$sid&errmsg=修改成功");
		else $this->_redirect("/scenic/scenic/addticket?sid=$sid&errmsg=修改失败");
	}
	/**
	 * @name 禁用门票
	 * @comment 禁用门票
	 */
	public function disableticketAction(){
		$tid=$this->_getParam('tid');
		$sid=$this->_getParam('sid');
		$ret=$this->scenic->disableTicket($tid);
		if($ret)	$this->_redirect("/scenic/scenic/addticket?sid=$sid&errmsg=禁用");
		else $this->_redirect("/scenic/scenic/addticket?sid=$sid&errmsg=禁用失败");
	}
	/**
	 * @name 启用门票
	 * @comment 启用门票
	 */
	public function enableticketAction(){
		$tid=$this->_getParam('tid');
		$sid=$this->_getParam('sid');
		$ret=$this->scenic->enableTicket($tid);
		if($ret)	$this->_redirect("/scenic/scenic/addticket?sid=$sid&errmsg=起用");
		else $this->_redirect("/scenic/scenic/addticket?sid=$sid&errmsg=起用失败");
	}
	/**
	 * @name 添加主题 
	 * @comment 添加主题
	 */
	public function addsubjectAction(){
		$sid=$this->_getParam('sid');
		$subArr=$this->scenic->getSubject();
		$scenicSubArr=$this->scenic->getSenicSubject($sid);
		
		foreach ($subArr as &$sub){
			$subid=$sub['subid'];
			$sub['sel']="";
			foreach ($scenicSubArr as $ssub){
				if($ssub['subid']==$subid){
					$sub['sel']="checked";
				}
			}
		}
		$this->view->subject=$subArr;
		$this->view->sid=$sid;
	}
	public function doaddsubAction(){
		$this->_helper->viewRenderer->setNoRender();
		$len=$this->_getParam('len');
		$sid=$this->_getParam('sid');
		$subArr=array();
		for($i=0;$i<$len;$i++){
			$subject=$this->_getParam('chk-'.$i);
			if($subject!=null&&$subject!='')	$subArr[]=$subject;
		}
		$this->scenic->addSenicSubject($subArr,$sid);
		$this->_redirect("/scenic/scenic/index?errmsg=修改主题成功");
	}
	public function uploadAction(){
		debug::output(1,"1111111111111");
		if (isset($_POST["PHPSESSID"])) {
			session_id($_POST["PHPSESSID"]);
		}
	
		if(!isset($_SESSION)){
    		session_start();
		}
		ini_set("html_errors", "0");
	
		// Check the upload
		// Check the upload
		if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
			echo "ERROR:invalid upload";
			exit(0);
		}
		if (!isset($_SESSION["file_info"])) {
			$_SESSION["file_info"] = array();
		}
		
		$fileName = md5(rand()*10000000) . ".jpg";
		move_uploaded_file($_FILES["Filedata"]["tmp_name"], "/tmp/" . $fileName);
	
		$file_id = md5(rand()*10000000);
		
		$_SESSION["file_info"][$file_id] = $fileName;
		
		debug::output(1,$_SESSION["file_info"]);
	
		echo "FILEID:" . $file_id;	// Return the file id to the script
	}
}